package com.zhz.company.biz;

import com.beust.jcommander.internal.Lists;
import com.zhz.common.tool.mybatisplus.base.core.contract.Page;
import com.zhz.common.tool.mybatisplus.base.core.contract.PageParam;
import com.zhz.common.utils.ConvertUtils;
import com.zhz.company.contract.request.*;
import com.zhz.company.contract.response.DepartmentFindAllResponse;
import com.zhz.company.contract.response.DepartmentFindByCodeResponse;
import com.zhz.company.contract.response.DepartmentFindByIdResponse;
import com.zhz.company.repository.service.CompanyService;
import com.zhz.company.repository.service.DepartmentService;
import com.zhz.model.company.contract.dto.CompanyDTO;
import com.zhz.model.company.contract.dto.DepartmentDTO;
import com.zhz.model.company.contract.param.CompanyQueryParam;
import com.zhz.model.company.contract.param.DepartmentQueryParam;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author zhouhengzhe
 * @date 2023/3/26
 */
@Component
public class DepartBizService {
    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private CompanyService companyService;

    /**
     * 添加部门
     *
     * @param departmentSaveRequest 保存部门实体
     */
    public void save(DepartmentSaveRequest departmentSaveRequest) {
        DepartmentDTO departmentDTO = ConvertUtils.sourceToTarget(departmentSaveRequest, DepartmentDTO.class);
        departmentService.save(departmentDTO);
    }

    /**
     * 指定企业id查询企业的部门列表
     *
     * @param companyId
     * @return
     */
    public DepartmentFindAllResponse findAll(String companyId) {
        //tg
        CompanyDTO companyDTO = getCompanyById(companyId);
        //2.完成查询
        DepartmentQueryParam departmentQueryParam = DepartmentQueryParam
                .builder()
                .companyId(companyId)
                .build();
        List<DepartmentDTO> departmentDTOS = departmentService.list(departmentQueryParam);
        //3.构造返回结果
        List<DepartmentFindAllResponse.DepartmentFindAllInfo> departmentFindAllInfos = Lists.newArrayList();
        if (CollectionUtils.isNotEmpty(departmentDTOS)) {
            departmentFindAllInfos = departmentDTOS.stream().map(i ->
                    ConvertUtils.sourceToTarget(i, DepartmentFindAllResponse.DepartmentFindAllInfo.class)
            ).collect(Collectors.toList());
        }
        return DepartmentFindAllResponse.builder()
                .depts(departmentFindAllInfos)
                .companyId(companyDTO.getId())
                .companyName(companyDTO.getName())
                .companyManage(companyDTO.getLegalRepresentative())
                .build();
    }

    /**
     * 根据ID获取公司信息
     *
     * @param companyId
     * @return
     */
    private CompanyDTO getCompanyById(String companyId) {
        CompanyQueryParam companyQueryParam = CompanyQueryParam
                .builder()
                .id(companyId)
                .build();
        //1.指定企业id
        return companyService.getOne(companyQueryParam);
    }

    /**
     * 根据企业ID单个查询部门信息
     *
     * @param departmentFindByIdRequest
     * @return
     */
    public DepartmentFindByIdResponse findById(DepartmentFindByIdRequest departmentFindByIdRequest) {
        DepartmentDTO departmentDTO = departmentService.get(departmentFindByIdRequest.getId());
        return ConvertUtils.sourceToTarget(departmentDTO, DepartmentFindByIdResponse.class);
    }

    /**
     * 更新部门信息
     *
     * @param departmentUpdateRequest
     */
    public void update(DepartmentUpdateRequest departmentUpdateRequest) {
        //1.根据id查询部门
        DepartmentQueryParam departmentQueryParam = DepartmentQueryParam
                .builder()
                .id(departmentUpdateRequest.getId())
                .build();
        DepartmentDTO departmentDTO = departmentService.getOne(departmentQueryParam);
        if (Objects.nonNull(departmentDTO)) {
            departmentDTO.setCode(departmentUpdateRequest.getCode())
                    .setIntroduce(departmentUpdateRequest.getIntroduce())
                    .setName(departmentUpdateRequest.getName());
            departmentService.update(departmentDTO);
        }
    }

    /**
     * 根据id删除部门
     *
     * @param departmentDeleteRequest
     */
    public void deleteById(DepartmentDeleteRequest departmentDeleteRequest) {
        departmentService.delete(departmentDeleteRequest.getId());
    }

    /**
     * 根据code码和公司Id获取部门信息
     *
     * @param departmentFindByCodeRequest
     * @return
     */
    public DepartmentFindByCodeResponse findByCode(DepartmentFindByCodeRequest departmentFindByCodeRequest) {
        DepartmentQueryParam departmentQueryParam = DepartmentQueryParam
                .builder()
                .code(departmentFindByCodeRequest.getCode())
                .companyId(departmentFindByCodeRequest.getCompanyId())
                .build();
        DepartmentDTO departmentDTO = departmentService.getOne(departmentQueryParam);
        return ConvertUtils.sourceToTarget(departmentDTO, DepartmentFindByCodeResponse.class);
    }

    /**
     * 分页查询部门信息
     *
     * @param departmentQueryParam
     * @return
     */
    public Page<DepartmentFindByIdResponse> page(DepartmentQueryParam departmentQueryParam) {
        PageParam<DepartmentQueryParam> pageParam = new PageParam<>();
        pageParam.setPage(departmentQueryParam.getPage())
                .setLimit(departmentQueryParam.getLimit())
                .setParam(departmentQueryParam);
        Page<DepartmentDTO> page = departmentService.page(pageParam);
        List<DepartmentFindByIdResponse> departmentFindByIdResponses = Lists.newArrayList();
        if (CollectionUtils.isNotEmpty(page.getList())) {
            departmentFindByIdResponses = page.getList().stream().map(departmentDTO ->
                    ConvertUtils.sourceToTarget(departmentDTO, DepartmentFindByIdResponse.class)
            ).collect(Collectors.toList());
        }

        return Page.succeed(departmentFindByIdResponses, page.getTotal());
    }
}
